<?php
/***CIL测试*****/
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Option;
/**
 * 批量生成，菜单权限控制的
 * 命令行： php think menu_group_rule --title=文案
 * 
 *
 * @Author 小邓
 * @DateTime 2022-11-16
 */
class MenuGroupRule extends Command
{
    protected function configure()
    {
        $this->setName('menu_group_rule')
        ->addOption('title',null, Option::VALUE_REQUIRED,  "主菜单名称", 'all')
        ->setDescription('自动生成菜单规则表记录');
    }
    private $subRuleArr = [
        'add'      => "增加",
        'del'      => "删除",
        'edit'     => "修改",
        'down_all' => "全库下载",
        'down'     => "下载",
        'upload'   => "上报公共库",
        'status'   => "过稿",
        'user'     => '工号',
        'distribution_receive'   => "下放-接收",
        'distribution_send'      => "下放-分配",
    ];

    //哪些菜单要加子权限
    private $subRule = [
        //文案
        "admin/material/wenan-index_1_1"      => ["add", 'del', 'edit', 'down_all'],
        "admin/material/wenan-hot_1_1"        => ['down_all'],
        "admin/material/wenan-inside_1_1"     => ["add", 'del', 'edit', 'down_all'],
        'admin/material/wenan-mycenter_1_1'   => ['del', 'edit', 'down_all', 'down', 'upload', 'status', 'user', 'distribution_receive', 'distribution_send'],

        //图片
        "admin/material/img-images_1_6"       => ["add", 'del', 'edit', 'down'],
        "admin/material/img-hot_1_6"          => ['down'],
        "admin/material/img-inside_1_6"       => ["add", 'del', 'edit', 'down'],
        'admin/material/img-mycenter_1_6'     => ["add", 'del', 'edit', 'down', 'upload', 'status', 'user', 'distribution_receive', 'distribution_send'],

        //视频
        "admin/material/video-shipi_1_3"      => ["add", 'del', 'edit', 'down'],
        "admin/material/video-hot_1_3"        => ['down'],
        "admin/material/video-inside_1_3"     => ["add", 'del', 'edit', 'down'],
        'admin/material/video-mycenter_1_3'   => ["add", 'del', 'edit', 'down', 'upload', 'status', 'user', 'distribution_receive', 'distribution_send'],

        //BGM
        "admin/material/bgm-index_1_13"      => ["add", 'del', 'edit', 'down'],
        "admin/material/bgm-inside_1_13"     => ["add", 'del', 'edit', 'down'],

        
        //书库 在跑书库
        "admin/book/adsing"      => ["add", 'del', 'edit'],

    ];

    protected function execute(Input $input, Output $output)
    {
        $title      = trim($input->getOption('title'));
        $map = [
            'id' => ['gt', 205],
            'pid'=> 0,
            "title" => $title
        ];
        if($title == 'all'){
            unset($map['title']);
        }
        $menuList =  db("menu")->where($map)->order("sort asc, id desc")->select();
        $groupRules = [];
        $actionInsertData = [];
        $subRuleArr = $this->subRuleArr;
        $subRule = $this->subRule;
        foreach ($menuList as $k => $v) {
            $submenu =  db("menu")->where("pid", $v['id'])->order("sort asc, id desc")->select();
            $group   = $v['title'];
            $groupRules[] = [
                'module'        => $v['type'],
                'type'          => 2,
                'name'          => $v['url'],
                'title'         => $v['title'],
                'group'         => $v['title'],
                'status'        => $v['status'] == 1 ? 0 : 1,
            ];
            foreach ($submenu as $k2 => $v2) {
                $subGroupRules = [
                    'module'        => $v2['type'],
                    'type'          => 2,
                    'name'          => $v2['url'],
                    'title'         => $v2['title'],
                    'group'         => $v['title'],
                    'status'        => $v2['status'] == 1 ? 0 : 1,
                ];
                array_push($groupRules, $subGroupRules);
                // echo $v2['url'];exit;
                //用户行为
                if( in_array($v2['url'],  array_keys($subRule)) ){
                    $userRule = $subRule[$v2['url']];
                    foreach ($userRule as $k3 => $v3) {
                        $userSubGroupRules = [
                            'module'        => $v2['type'],
                            'type'          => 0,
                            'name'          => $v2['url'].'_'.$v3,
                            'title'         => $v2['title'].'-'.$subRuleArr[$v3],
                            'group'         => $v['title']."-用户操作",
                            'status'        => $v2['status'] == 1 ? 0 : 1,
                        ];
                        array_push($groupRules, $userSubGroupRules);

                        //保存到 日记行为列表
                        $map = [
                            'name' => $v2['url'].'_'.$v3,
                        ];
                        $action_id = db("action")->where($map)->value('id');
                        $actionInsertData[]  = [
                            "id" => $action_id?:null,
                            'name' => $v2['url'].'_'.$v3,
                            "title" => $v['title']."-".$v2['title'].'-'.$subRuleArr[$v3],
                            "remark" => $v['title']."-用户操作",
                            'type' => 2,
                            'status' => 1,
                            'update_time' => time(),
                            'log' => '[user|get_nickname]在[time|time_format]'.$subRuleArr[$v3].'了[platform]文案,ids: [ids]'
                        ];
                    }

                }
            }
        }
        foreach ($groupRules as $k3 => $v3) {
            $map = [
                'type' => $v3['type'],
                'name' => $v3['name'],
                'title' => $v3['title'],
                'group' => $v3['group'],
            ];
            $authRuleId = db("auth_rule")->where($map)->value('id');
            $groupRules[$k3]['id'] = $authRuleId ?: null;
        }
        $model =  new \app\common\model\AuthRule();
        $model->isUpdate()->saveAll($groupRules);
        echo db("auth_rule")->getLastSql();

        $model =  new \app\common\model\Action();
        $model->isUpdate()->saveAll($actionInsertData);
        echo db("action")->getLastSql();
    }
}